/*
 * @@@@@@@@@@@@@
 * KANG FEI: DO NOT MODIFY THIS FILE, USE Test.java INSTEAD
 * @@@@@@@@@@@@@
 */

package com.it2297.main;

import java.awt.BorderLayout;



import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.border.EmptyBorder;

import com.it2297.controller.BiddersController;
import com.it2297.controller.BiddingHistoryController;
import com.it2297.controller.DonationHistoryController;
import com.it2297.controller.DonationReportController;
import com.it2297.controller.ProductController;
import com.it2297.controller.UsedItemController;
import com.it2297.model.Customer;
import com.it2297.model.Needy;
import com.it2297.model.User;
import com.it2297.util.MySQLConnection;
import com.it2297.util.VTextIcon;
import com.it2297.view.CreateWorkshopView;
import com.it2297.view.CustomerOrderListDialog;
import com.it2297.view.DeleteWorkshopView;
import com.it2297.view.DonationHistoryView;
import com.it2297.view.DonationReportView;
import com.it2297.view.LoginView;
import com.it2297.view.ManageApplication;
import com.it2297.view.NeedyDeleteView;
import com.it2297.view.NeedyProfileView;
import com.it2297.view.NeedySearchView;
import com.it2297.view.OrderHistory;
import com.it2297.view.OrderPaid;
import com.it2297.view.PaymentView;
import com.it2297.view.RetrieveProductView;
import com.it2297.view.UpdateWorkshopView;
import com.it2297.view.UsedItemSearchDialog;
import com.it2297.view.BiddersBrowseView;
import com.it2297.view.BiddersDetailDialog;
import com.it2297.view.BiddingHistoryView;
import com.it2297.view.CompanyCreateCompany;
import com.it2297.view.CompanyUpdateCompany;
import com.it2297.view.CreateCustomerView;
import com.it2297.view.DeleteCustomerView;
import com.it2297.view.JobRemoveApplication;
import com.it2297.view.JobSearchJob;
import com.it2297.view.NeedyView;
import com.it2297.view.NewShopView;
import com.it2297.view.SearchCustomerView;
import com.it2297.view.ShoppingCartView;
import com.it2297.view.UpdateCustomerView;
import com.it2297.view.UpdateNeedyView;
import com.it2297.view.UsedItemBrowseView;
import com.it2297.view.UsedItemCreateUpdateDialog;
import com.it2297.view.UsedItemDetailDialog;
import com.it2297.view.ViewCustomerView;
import com.it2297.view.ViewShortlistedApplications;
import com.it2297.view.WorkshopParticipatedView;
import com.it2297.view.WorkshopSearchView;
import com.it2297.view.WorkshopView;

@SuppressWarnings("serial")
public class App extends JFrame
{
	public static final MySQLConnection mySqlConnection = MySQLConnection.getInstance();
	public static User currentUser;
	public static Needy currentNeedy;
	public static Customer currentCustomer;
	/*
	private final UsedItemBrowseView usedItemBrowseView = new UsedItemBrowseView();
	private final UsedItemCreateUpdateDialog usedItemCreateUpdateDialog = new UsedItemCreateUpdateDialog();
	private final UsedItemDetailDialog usedItemDetailDialog = new UsedItemDetailDialog();
	private final UsedItemSearchDialog usedItemSearchDialog = new UsedItemSearchDialog();
	private final UsedItemController usedItemController = new UsedItemController(usedItemBrowseView, usedItemCreateUpdateDialog, usedItemDetailDialog, usedItemSearchDialog);
	
	private final BiddersBrowseView biddersBrowseView = new BiddersBrowseView();
	private final BiddersDetailDialog biddersDetailDialog = new BiddersDetailDialog();
	private final BiddersController biddersController = new BiddersController(biddersBrowseView, biddersDetailDialog);

	private final BiddingHistoryView biddingHistoryView = new BiddingHistoryView();
	private final BiddingHistoryController biddingHistoryController = new BiddingHistoryController(biddingHistoryView);

	private final DonationReportView donationReportView = new DonationReportView();
	private final DonationReportController donationReportController = new DonationReportController(donationReportView);

	private final DonationHistoryView donationHistoryView = new DonationHistoryView();
	private final DonationHistoryController donationHistoryController = new DonationHistoryController(donationHistoryView);
	*/
	private final ProductController pc = new ProductController();
	private final NewShopView newShopView = new NewShopView();
	private final ShoppingCartView shoppingCartView = new ShoppingCartView();
	private final PaymentView payView = new PaymentView();
	private final OrderHistory orderhistory = new OrderHistory();
	private final RetrieveProductView retrieve = new RetrieveProductView();
	private final OrderPaid oldOrder = new OrderPaid();
	
	/*
	private final NeedyView needyView = new NeedyView();
	private final NeedyProfileView profileView = new NeedyProfileView();
	private final UpdateNeedyView updateNeedyView = new UpdateNeedyView();
	private final NeedyDeleteView deleteNeedy = new NeedyDeleteView();
	private final NeedySearchView searchNeedy = new NeedySearchView();

	private final JobSearchJob jobSearchJob = new JobSearchJob();
	//private final JobRemoveApplication jobRemoveApplication = new JobRemoveApplication();
	private final ManageApplication ma = new ManageApplication();
	private final ViewShortlistedApplications viewShort = new ViewShortlistedApplications();

	private final UpdateCustomerView updateCustomerView = new UpdateCustomerView();
	private final DeleteCustomerView deleteCustomerView = new DeleteCustomerView();
	private final ViewCustomerView viewCustomerView = new ViewCustomerView();
	private final SearchCustomerView searchCustomerView = new SearchCustomerView();
	
	private final CreateWorkshopView createworkview = new CreateWorkshopView();
	private final DeleteWorkshopView delworkview = new DeleteWorkshopView();
	private final UpdateWorkshopView upworkview = new UpdateWorkshopView();
	private final WorkshopParticipatedView participated = new WorkshopParticipatedView();
	private final WorkshopSearchView searchview = new WorkshopSearchView();
	private final WorkshopView workshopview = new WorkshopView();
	*/
	
	
	private final JPanel contentPane = new JPanel();;
	private final JPanel body = new JPanel();
	private final JTabbedPane bodyPrimaryTabs = new JTabbedPane(JTabbedPane.LEFT);
	private final JPanel home = new JPanel();
	private final JPanel eShop = new JPanel();
	private final JTabbedPane eShopSecondaryTabs = new JTabbedPane(JTabbedPane.TOP);
	private final JPanel WorkShop = new JPanel();
	private final JTabbedPane WorkShopSecondaryTabs = new JTabbedPane(JTabbedPane.TOP);
	private final JMenuBar menuBar = new JMenuBar();
	private final JMenu mnFile = new JMenu("File");
	private final JMenuItem mntmOpen = new JMenuItem("Open");
	private final JMenuItem mntmSave = new JMenuItem("Save");
	private final JMenuItem mntmExit = new JMenuItem("Exit");
	private final JMenu mnEdit = new JMenu("Edit");
	private final JMenu mnHelp = new JMenu("Help");
	private final JMenuItem mntmAbout = new JMenuItem("About");
	private final JSeparator separator = new JSeparator();
	private final JPanel usedItem = new JPanel();
	private final JTabbedPane usedItemSecondaryTabs = new JTabbedPane(JTabbedPane.TOP);
	private final JPanel sidebar = new JPanel();
	private final JTabbedPane recommendation = new JTabbedPane(JTabbedPane.TOP);
	private final JPanel panel_5 = new JPanel();
	private final JTabbedPane announcement = new JTabbedPane(JTabbedPane.TOP);
	private final JPanel panel_8 = new JPanel();
	private final JPanel panel = new JPanel();
	private final JPanel needyPanel = new JPanel();
	private final JTabbedPane needyProfileSecondaryTab = new JTabbedPane(JTabbedPane.TOP);
	private final JTabbedPane careerSecondaryTabs = new JTabbedPane(JTabbedPane.TOP);
	private final JPanel customerPanel = new JPanel();
	private final JTabbedPane customerSecondaryTabs = new JTabbedPane(JTabbedPane.TOP);
	
	private static App main;
	
	public static App getMain(){
		return main;
	}
	
	public App()
	{
		main = this;
		initGUI();
	}

	private void initGUI()
	{
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setBounds(0, 0, 1000, 700);
		this.setLocationRelativeTo(null);
		this.setResizable(false);

		setJMenuBar(menuBar);
		mnFile.setForeground(Color.BLACK);
		mnFile.setBorderPainted(true);

		menuBar.add(mnFile);

		mnFile.add(mntmOpen);

		mnFile.add(mntmSave);

		mnFile.add(separator);
		mntmExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				logOut();
			}
		});
		mnFile.add(mntmExit);
		mnEdit.setForeground(Color.BLACK);

		menuBar.add(mnEdit);
		mnHelp.setForeground(Color.BLACK);

		menuBar.add(mnHelp);

		mnHelp.add(mntmAbout);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		contentPane.add(body, BorderLayout.CENTER);
		body.setLayout(new CardLayout(0, 0));

		body.add(bodyPrimaryTabs, "name_5761730689202");
		if(UserApp.getCurrentUser().getRole().equals("ADMIN")){
			/*//Customer
			bodyPrimaryTabs.addTab(null, null, customerPanel, null);
			customerPanel.setLayout(new CardLayout(0, 0));
			customerPanel.add(customerSecondaryTabs, "name_3150441337666");
			customerSecondaryTabs.addTab("Search Customer", null, searchCustomerView, null);
			customerSecondaryTabs.addTab("Delete Customer", null, deleteCustomerView, null);
			bodyPrimaryTabs.setIconAt(0, new VTextIcon(bodyPrimaryTabs, "Customer Profile", VTextIcon.ROTATE_RIGHT));
			bodyPrimaryTabs.setFocusable(false);
			//Needy
			bodyPrimaryTabs.addTab(null, null, needyPanel, null);
			needyPanel.setLayout(new CardLayout(0, 0));
			needyProfileSecondaryTab.addTab("Create Needy", null, needyView, null);
			needyProfileSecondaryTab.addTab("Update Needy", null, updateNeedyView, null);
			needyProfileSecondaryTab.addTab("Delete Needy", null, deleteNeedy, null);
			needyProfileSecondaryTab.addTab("Search Needy", null, searchNeedy, null);
			needyPanel.add(needyProfileSecondaryTab, "name_2255850279526");
			bodyPrimaryTabs.setIconAt(1, new VTextIcon(bodyPrimaryTabs, "Needy Profile", VTextIcon.ROTATE_RIGHT));
			*///e-shop
			bodyPrimaryTabs.addTab(null, null, eShop, null);
			eShop.setLayout(new CardLayout(0, 0));
			eShopSecondaryTabs.setFocusable(false);
			eShop.add(eShopSecondaryTabs, "name_6762871510460");
			eShopSecondaryTabs.addTab("E-Shop", null, newShopView, null);
			eShopSecondaryTabs.addTab("Payment", null, payView, null);
			eShopSecondaryTabs.addTab("Retrieve Product", null, retrieve, null);
			bodyPrimaryTabs.setIconAt(0, new VTextIcon(bodyPrimaryTabs, "E-Shop", VTextIcon.ROTATE_RIGHT));
			bodyPrimaryTabs.setFocusable(false);
			/*
			//workshop
			bodyPrimaryTabs.addTab(null, null, WorkShop, null);
			WorkShop.setLayout(new CardLayout(0, 0));
			WorkShopSecondaryTabs.addTab("Create WorkShop" , null ,createworkview , null);
			WorkShopSecondaryTabs.addTab("Delete WorkShop" , null ,delworkview , null);
			WorkShopSecondaryTabs.addTab("Update WorkShop" , null ,upworkview , null);
			WorkShopSecondaryTabs.addTab("Search Work Shop" , null , searchview, null);
			WorkShopSecondaryTabs.addTab("View WorkShop" , null ,workshopview , null);
			WorkShop.add(WorkShopSecondaryTabs, "name_3150441337688");
			bodyPrimaryTabs.setIconAt(3, new VTextIcon(bodyPrimaryTabs, " Work Shop", VTextIcon.ROTATE_RIGHT));
			*/
		}else if (UserApp.getCurrentUser().getRole().equals("CUSTOMER")){
			
			/*//CUSTOMER
			bodyPrimaryTabs.addTab(null, null, customerPanel, null);
			customerPanel.setLayout(new CardLayout(0, 0));
			customerPanel.add(customerSecondaryTabs, "name_3150441337666");
			customerSecondaryTabs.addTab("Update Customer", null, updateCustomerView, null);
			customerSecondaryTabs.addTab("My Page", null, viewCustomerView, null);
			bodyPrimaryTabs.setIconAt(0, new VTextIcon(bodyPrimaryTabs, "Customer Profile", VTextIcon.ROTATE_RIGHT));
			bodyPrimaryTabs.setFocusable(false);
			*/
			//ESHOP
			bodyPrimaryTabs.addTab(null, null, eShop, null);
			eShop.setLayout(new CardLayout(0, 0));
			eShopSecondaryTabs.setFocusable(false);
			eShop.add(eShopSecondaryTabs, "name_6762871510460");
			eShopSecondaryTabs.addTab("E-Shop", null, newShopView, null);
			eShopSecondaryTabs.addTab("Shopping Cart", null, shoppingCartView, null);
			eShopSecondaryTabs.addTab("Order list", null, orderhistory, null);
			eShopSecondaryTabs.addTab("Order History", null, oldOrder, null);
			bodyPrimaryTabs.setIconAt(0, new VTextIcon(bodyPrimaryTabs, "E-Shop", VTextIcon.ROTATE_RIGHT));
			bodyPrimaryTabs.setFocusable(false);
			/*
		}else if(UserApp.getCurrentUser().getRole().equals("NEEDY")){
			/*
			//NEEDY
			bodyPrimaryTabs.addTab(null, null, needyPanel, null);
			needyPanel.setLayout(new CardLayout(0, 0));
			needyProfileSecondaryTab.addTab("Needy profile", null, profileView, null);
			needyPanel.add(needyProfileSecondaryTab, "name_2255850279526");
			bodyPrimaryTabs.setIconAt(0, new VTextIcon(bodyPrimaryTabs, "Needy Profile", VTextIcon.ROTATE_RIGHT));
			
			//CAREER
			bodyPrimaryTabs.addTab(null, null, panel, null);
			panel.setLayout(new CardLayout(0, 0));
			panel.add(careerSecondaryTabs, "name_1542559002061");
			careerSecondaryTabs.addTab("Search Job", null, jobSearchJob, null);
			careerSecondaryTabs.addTab("View ShortListed", null, viewShort, null);
			careerSecondaryTabs.addTab("Manage Application", null, ma, null);
			bodyPrimaryTabs.setIconAt(0, new VTextIcon(bodyPrimaryTabs, "Career", VTextIcon.ROTATE_RIGHT));
			
			//WORKSHOP
			bodyPrimaryTabs.addTab(null, null, WorkShop, null);
			WorkShop.setLayout(new CardLayout(0, 0));
			WorkShop.add(WorkShopSecondaryTabs, "name_3150441337688");
			bodyPrimaryTabs.setIconAt(2, new VTextIcon(bodyPrimaryTabs, " Work Shop", VTextIcon.ROTATE_RIGHT));
			WorkShopSecondaryTabs.addTab("Search WorkShop" , null , searchview, null);
			WorkShopSecondaryTabs.addTab("View WorkShop" , null ,workshopview , null);
			WorkShopSecondaryTabs.addTab("Participate Work Shop" , null ,participated , null);
			
			
			//Donation
			bodyPrimaryTabs.addTab(null, null,  usedItem , null);
			 usedItem.setLayout(new CardLayout(0, 0));
			 usedItemSecondaryTabs.setLayout(new CardLayout(0, 0));
			 bodyPrimaryTabs.setIconAt(3, new VTextIcon(bodyPrimaryTabs, "Donation", VTextIcon.ROTATE_RIGHT));
			 usedItemSecondaryTabs.addTab("View Item" , null ,usedItemBrowseView  , null);
			*/
		}
		
		/*
		bodyPrimaryTabs.addTab(null, null, home, null);
		home.setLayout(new CardLayout(0, 0));
		bodyPrimaryTabs.setIconAt(0, new VTextIcon(bodyPrimaryTabs, "Home Page", VTextIcon.ROTATE_RIGHT));

		bodyPrimaryTabs.addTab(null, null, eShop, null);
		eShop.setLayout(new CardLayout(0, 0));
		eShopSecondaryTabs.setFocusable(false);

		eShop.add(eShopSecondaryTabs, "name_6762871510460");

			eShopSecondaryTabs.addTab("E-Shop", null, newShopView, null);
		if(UserApp.getCurrentUser().getRole().equals("ADMIN")){
			eShopSecondaryTabs.addTab("Payment", null, payView, null);
			eShopSecondaryTabs.addTab("Retrieve Product", null, retrieve, null);
		}else{
			eShopSecondaryTabs.addTab("Shopping Cart", null, shoppingCartView, null);
			eShopSecondaryTabs.addTab("Order History", null, orderhistory, null);
		}

		bodyPrimaryTabs.setIconAt(1, new VTextIcon(bodyPrimaryTabs, "E-Shop", VTextIcon.ROTATE_RIGHT));
		bodyPrimaryTabs.setFocusable(false);

		bodyPrimaryTabs.addTab(null, null, usedItem, null);
		usedItem.setLayout(new CardLayout(0, 0));

		usedItem.add(usedItemSecondaryTabs, "name_35896460157636");

		usedItemSecondaryTabs.setFocusable(false);

		bodyPrimaryTabs.setIconAt(2, new VTextIcon(bodyPrimaryTabs, "Donations", VTextIcon.ROTATE_RIGHT));

		bodyPrimaryTabs.addTab(null, null, panel, null);
		panel.setLayout(new CardLayout(0, 0));

		panel.add(careerSecondaryTabs, "name_1542559002061");
		
		if(UserApp.getCurrentUser().getRole().equals("NEEDY")){
		careerSecondaryTabs.addTab("Search Job", null, jobSearchJob, null);
		careerSecondaryTabs.addTab("Remove Application", null, jobRemoveApplication, null);
		}

		bodyPrimaryTabs.setIconAt(3, new VTextIcon(bodyPrimaryTabs, "Career", VTextIcon.ROTATE_RIGHT));
		
		bodyPrimaryTabs.addTab(null, null, customerPanel, null);
		customerPanel.setLayout(new CardLayout(0, 0));

		customerPanel.add(customerSecondaryTabs, "name_3150441337666");
		bodyPrimaryTabs.setIconAt(4, new VTextIcon(bodyPrimaryTabs, "Customer Profile", VTextIcon.ROTATE_RIGHT));
		
		customerSecondaryTabs.addTab("Update Customer", null, updateCustomerView, null);
		customerSecondaryTabs.addTab("My Page", null, viewCustomerView, null);
		if(UserApp.getCurrentUser().getRole().equals("ADMIN")){
		customerSecondaryTabs.addTab("Search Customer", null, searchCustomerView, null);
		customerSecondaryTabs.addTab("Delete Customer", null, deleteCustomerView, null);
		}
		
		bodyPrimaryTabs.addTab(null, null, WorkShop, null);
		WorkShop.setLayout(new CardLayout(0, 0));
		
		WorkShop.add(WorkShopSecondaryTabs, "name_3150441337688");
		bodyPrimaryTabs.setIconAt(5, new VTextIcon(bodyPrimaryTabs, " Work Shop", VTextIcon.ROTATE_RIGHT));
		
		WorkShopSecondaryTabs.addTab("Create WorkShop" , null ,createworkview , null);
		WorkShopSecondaryTabs.addTab("Delete WorkShop" , null ,delworkview , null);
		WorkShopSecondaryTabs.addTab("Update WorkShop" , null ,upworkview , null);
		WorkShopSecondaryTabs.addTab("View WorkShop" , null ,workshopview , null);
		WorkShopSecondaryTabs.addTab("Participate Work Shop" , null ,participated , null);
		WorkShopSecondaryTabs.addTab("Search Work Shop" , null , searchview, null);
		
		if(UserApp.getCurrentUser().getRole().equals("ADMIN")){
				bodyPrimaryTabs.addTab(null, null, needyPanel, null);
				needyPanel.setLayout(new CardLayout(0, 0));
				
				needyPanel.add(needyProfileSecondaryTab, "name_2255850279526");
				bodyPrimaryTabs.setIconAt(6, new VTextIcon(bodyPrimaryTabs, "Needy Profile", VTextIcon.ROTATE_RIGHT));

				needyProfileSecondaryTab.addTab("Create Needy", null, needyView, null);
				needyProfileSecondaryTab.addTab("Update Needy", null, updateNeedyView, null);
				needyProfileSecondaryTab.addTab("Delete Needy", null, deleteNeedy, null);
				needyProfileSecondaryTab.addTab("Search Needy", null, searchNeedy, null);
				}
		*/
		contentPane.add(sidebar, BorderLayout.EAST);
		GridBagLayout gbl_sidebar = new GridBagLayout();
		gbl_sidebar.columnWidths = new int[] { 200, 0 };
		gbl_sidebar.rowHeights = new int[] { 297, 319, 0 };
		gbl_sidebar.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
		gbl_sidebar.rowWeights = new double[] { 1.0, 1.0, Double.MIN_VALUE };
		sidebar.setLayout(gbl_sidebar);

		GridBagConstraints gbc_announcement = new GridBagConstraints();
		gbc_announcement.insets = new Insets(0, 0, 5, 0);
		gbc_announcement.fill = GridBagConstraints.BOTH;
		gbc_announcement.gridx = 0;
		gbc_announcement.gridy = 0;
		announcement.setFocusable(false);
		sidebar.add(announcement, gbc_announcement);

		announcement.addTab("New tab", null, panel_8, null);

		GridBagConstraints gbc_recommendation = new GridBagConstraints();
		gbc_recommendation.fill = GridBagConstraints.BOTH;
		gbc_recommendation.gridx = 0;
		gbc_recommendation.gridy = 1;
		recommendation.setFocusable(false);
		sidebar.add(recommendation, gbc_recommendation);

		recommendation.addTab("New tab", null, panel_5, null);

	}
	/*
	public void authenticate()
	{

		String role = App.currentUser.getRole();

		if (role.equals("ADMIN"))
		{
			// kf's
			usedItemBrowseView.addAdminToolBar();

			usedItemSecondaryTabs.addTab("Browse", null, usedItemBrowseView, null);
			usedItemSecondaryTabs.addTab("Bidded Items", null, biddersBrowseView, null);
			usedItemSecondaryTabs.addTab("Report", null, donationReportView, null);
			// end of kf's
		}
		else if (role.equals("CUSTOMER"))
		{
			// kf's
			usedItemSecondaryTabs.addTab("Browse", null, usedItemBrowseView, null);
			usedItemSecondaryTabs.addTab("Donation History", null, donationHistoryView, null);
			// end of kf's
		}
		else if (role.equals("NEEDY"))
		{
			// kf's
			usedItemDetailDialog.addBidButton();

			usedItemSecondaryTabs.addTab("Browse", null, usedItemBrowseView, null);
			usedItemSecondaryTabs.addTab("Bidding History", null, biddingHistoryView, null);
			// end of kf's
		}
		else
		{
			System.out.println("Unidentified user! Quitting application.");
			System.exit(0);
		}
	}
	*/
	public void logOut(){
		//System.gc();
		pc.clear();
		this.dispose();
		LoginView view = new LoginView();
		view.setVisible(true);
	}
	
}
